
// pages/schedule/schedule.js
Page({
  data: {
    inputValue: '', // 输入框内容
    todos: [],      // 待办事项列表
    history: [],    // 历史记录列表
    completedCount: 0, // 已完成数量
    showHistory: false, // 是否显示历史记录
    currentTab: 'todos' // 当前选中的标签页：todos 或 history
  },

  // 输入框内容变化
  inputChange: function(e) {
    this.setData({
      inputValue: e.detail.value
    })
  },

  // 添加待办事项
  addTodo: function() {
    if (!this.data.inputValue.trim()) return
    
    const newTodo = {
      id: Date.now(),
      text: this.data.inputValue,
      completed: false,
      createTime: new Date().toLocaleString('zh-CN')
    }
    
    this.setData({
      todos: [newTodo, ...this.data.todos],
      inputValue: ''
    })
    
    this.updateStats()
    wx.showToast({
      title: '添加成功',
      icon: 'success',
      duration: 1000
    })
  },

  // 删除待办事项
  deleteTodo: function(e) {
    const id = e.currentTarget.dataset.id
    const todo = this.data.todos.find(item => item.id === id)
    
    if (todo) {
      // 添加到历史记录
      const historyItem = {
        ...todo,
        deletedTime: new Date().toLocaleString('zh-CN'),
        historyId: Date.now()
      }
      
      this.setData({
        todos: this.data.todos.filter(item => item.id !== id),
        history: [historyItem, ...this.data.history]
      })
      
      this.updateStats()
      this.saveHistory()
      wx.showToast({
        title: '删除成功',
        icon: 'success',
        duration: 1000
      })
    }
  },

  // 切换完成状态
  toggleTodo: function(e) {
    const id = e.currentTarget.dataset.id
    this.setData({
      todos: this.data.todos.map(item => {
        if (item.id === id) {
          const updatedItem = { ...item, completed: !item.completed }
          if (!item.completed) {
            updatedItem.completeTime = new Date().toLocaleString('zh-CN')
          }
          return updatedItem
        }
        return item
      })
    })
    this.updateStats()
  },

  // 更新统计信息
  updateStats: function() {
    this.setData({
      completedCount: this.data.todos.filter(item => item.completed).length
    })
    
    // 保存到本地存储
    wx.setStorageSync('todos', this.data.todos)
  },

  // 保存历史记录
  saveHistory: function() {
    // 只保留最近100条历史记录
    const recentHistory = this.data.history.slice(0, 100)
    wx.setStorageSync('todoHistory', recentHistory)
    this.setData({ history: recentHistory })
  },

  // 加载本地存储的数据
  onLoad: function() {
    const todos = wx.getStorageSync('todos') || []
    const history = wx.getStorageSync('todoHistory') || []
    this.setData({ todos, history })
    this.updateStats()
  },

  // 切换标签页
  switchTab: function(e) {
    const tab = e.currentTarget.dataset.tab
    this.setData({ currentTab: tab })
  },

  // 清空历史记录
  clearHistory: function() {
    wx.showModal({
      title: '确认清空',
      content: '确定要清空所有历史记录吗？',
      success: res => {
        if (res.confirm) {
          this.setData({ history: [] })
          wx.removeStorageSync('todoHistory')
          wx.showToast({
            title: '历史记录已清空',
            icon: 'success',
            duration: 1000
          })
        }
      }
    })
  },

  // 从历史记录恢复
  restoreFromHistory: function(e) {
    const historyId = e.currentTarget.dataset.id
    const historyItem = this.data.history.find(item => item.historyId === historyId)
    
    if (historyItem) {
      // 创建新的待办事项，使用新的ID
      const restoredTodo = {
        id: Date.now(),
        text: historyItem.text,
        completed: false,
        createTime: new Date().toLocaleString('zh-CN')
      }
      
      this.setData({
        todos: [restoredTodo, ...this.data.todos],
        history: this.data.history.filter(item => item.historyId !== historyId)
      })
      
      this.updateStats()
      this.saveHistory()
      wx.showToast({
        title: '已恢复',
        icon: 'success',
        duration: 1000
      })
    }
  }
})